******************
* 1. Basic codings
******************
	
	*****************************
	* FILTER DK	
	*****************************
	* filter in which the municipalities of Copenhagen, Frederiksberg, Bornholm and Ærø are filtered out
	cap drop filter_gen
	gen filter_gen = 1 
	recode filter_gen (1=0) if 	knr == 101 | knr == 147 | knr == 400 | knr == 492
	tab filter_gen

	*****************************
	* FILTER NO
	*****************************
	* filter in which the municipalities of Oslo, Utsira, 10 powerplant municipalities and 2111 Longyearbyen are filtered out
	recode filter_gen (1=0) if 	knr2019 == 301 | knr2019 == 1151 | knr2019 == 2111
	recode filter_gen (1=0) if 	kraftkommune2019 == 1 
	* filter in which the municipalities amalgamated in 2017 and 2018 are filtered out
	recode filter_gen (1=0) if 	amalgamation_year == 2017 | amalgamation_year == 2018
	recode filter_gen (1=0) if 	knr2019 == 710 | knr2019 == 712 | knr2019 == 715 | knr2019 == 729 | knr2019 == 5054
	tab filter_gen
	
	*****************************	
	* Macros for variables
	*****************************

	global yeardum i.year_of_analysis
	global amalgam i.amalgamation i.year_of_analysis##i.amalgamation##country
	global amalgam_popshare popshare_of_new_mun i.year_of_analysis##c.popshare_of_new_mun##country

	global amalgam_one_country i.amalgamation i.year##i.amalgamation
	global amalgam_popshare_one_country popshare_of_new_mun i.year##c.popshare_of_new_mun
	
	global context_var i.country wealth_pc_fp_EUR ln_pop rightwing_share
	global context_var_one_country wealth_pc_fp_EUR ln_pop rightwing_share
	
	global context_popshare i.country wealth_pc_fp_EUR ln_pop_newmun rightwing_share
	global context_popshare_one_country wealth_pc_fp_EUR ln_pop_newmun rightwing_share


	
*************************
* 2. Analyses	
*************************
	
**************************		
* I. SUMMARIZE and TABELS
**************************
	tab country if filter_gen==1

* TABLE A1a Measurement of variables and descriptive statistics 2015-2019. Norway
	sum net_current_exp_pc_fp_EUR gross_investments_pc_fp_EUR netresult_pc_fp_EUR liquidassets_pc_fp_EUR longtermdebt_pc_fp_EUR wealth_pc_fp_EUR ln_pop rightwing_share popshare_of_new_mun ln_pop_newmun  if filter_gen==1 & country == 1
* TABLE A1b Measurement of variables and descriptive statistics 2002-2006. Denmark
	sum net_current_exp_pc_fp_EUR gross_investments_pc_fp_EUR netresult_pc_fp_EUR liquidassets_pc_fp_EUR longtermdebt_pc_fp_EUR wealth_pc_fp_EUR ln_pop rightwing_share popshare_of_new_mun ln_pop_newmun  if filter_gen==1 & country == 0
	
* TABLE A2. Descriptive statistics (means) for merged and non-merged municipalities in Norway and Denmark
	*descriptive statistics for merged og non-merged in Denmark
	global all_y net_current_exp_pc_fp_EUR gross_investments_pc_fp_EUR netresult_pc_fp_EUR liquidassets_pc_fp_EUR longtermdebt_pc_fp_EUR wealth_pc_fp_EUR ln_pop rightwing_share
	set more off
		foreach Y of varlist $all_y {
			tab amalgamation if filter_gen==1 & country == 0, s(`Y')
		}
	*descriptive statistics for merged og non-merged in Norway
	global all_y net_current_exp_pc_fp_EUR gross_investments_pc_fp_EUR netresult_pc_fp_EUR liquidassets_pc_fp_EUR longtermdebt_pc_fp_EUR wealth_pc_fp_EUR ln_pop rightwing_share
	set more off
		foreach Y of varlist $all_y {
			tab amalgamation if filter_gen==1 & country == 1, s(`Y')
		}
	set more on	

	
**************************************************************************
* IIa. DID-Analyses - For NORWAY
**************************************************************************

* TABLE 2 - for Norway
* TABLE A3a. DiD analysis of pre-merger overspending, merging vs. non-merging municipalities. Norway

	global all_y net_current_exp_pc_fp_EUR gross_investments_pc_fp_EUR netresult_pc_fp_EUR liquidassets_pc_fp_EUR longtermdebt_pc_fp_EUR 
	
		set more off
		foreach Y of varlist $all_y {
		
		reg `Y' ///
			$amalgam_one_country ///
			i.year ///
			$context_var_one_country ///
			if filter_gen==1 & country == 1, cluster(ct_knr)
			
			fitstat
		
		estat vif
		local max_vif = `r(vif_1)'

		local r_sq_adj = e(r2_a)
		
		outreg2 using overspending_2015-2019_NO, addstat(Adj. R-squared, `r_sq_adj',max VIF, `max_vif') adec(3) bdec(2) sdec(2) word
		}
		
		set more on

* TABLE A5a. DiD analysis of pre-merger overspending, differentiated by the size of the municipality relative to the merged unit. Norway
		
	global all_y net_current_exp_pc_fp_EUR gross_investments_pc_fp_EUR netresult_pc_fp_EUR liquidassets_pc_fp_EUR longtermdebt_pc_fp_EUR 
	
		set more off
		foreach Y of varlist $all_y {
		
		reg `Y' ///
			$amalgam_popshare_one_country ///
			i.year  ///
			$context_popshare_one_country ///
			if filter_gen==1 & country == 1, cluster(ct_knr)
			
			fitstat
		
		estat vif
		local max_vif = `r(vif_1)'

		local r_sq_adj = e(r2_a)
		
		outreg2 using overspending_2015-2019_popshare_NO, addstat(Adj. R-squared, `r_sq_adj',max VIF, `max_vif') adec(3) bdec(2) sdec(2) word
		}
		set more on
	
	
**************************************************************************
* IIb. DID-Analyses - For DENMARK
**************************************************************************

* TABLE 2 - for Denmark
* TABLE A3b. DiD analysis of pre-merger overspending, merging vs. non-merging municipalities. Denmark

	global all_y net_current_exp_pc_fp_EUR gross_investments_pc_fp_EUR netresult_pc_fp_EUR liquidassets_pc_fp_EUR longtermdebt_pc_fp_EUR 
	
		set more off
		foreach Y of varlist $all_y {
		
		reg `Y' ///
			$amalgam_one_country ///
			i.year ///
			$context_var_one_country ///
			if filter_gen==1 & country == 0, cluster(ct_knr)
			
			fitstat
		
		estat vif
		local max_vif = `r(vif_1)'

		local r_sq_adj = e(r2_a)
		
		outreg2 using overspending_2002-2006_DK, addstat(Adj. R-squared, `r_sq_adj',max VIF, `max_vif') adec(3) bdec(2) sdec(2) word
		}
		set more on
		
* TABLE A5b. DiD analysis of pre-merger overspending, differentiated by the size of the municipality relative to the merged unit. Denmark
		
	global all_y net_current_exp_pc_fp_EUR gross_investments_pc_fp_EUR netresult_pc_fp_EUR liquidassets_pc_fp_EUR longtermdebt_pc_fp_EUR 
	
		set more off
		foreach Y of varlist $all_y {
		
		reg `Y' ///
			$amalgam_popshare_one_country ///
			i.year  ///
			$context_popshare_one_country ///
			if filter_gen==1 & country == 0, cluster(ct_knr)
			
			fitstat
		
		estat vif
		local max_vif = `r(vif_1)'

		local r_sq_adj = e(r2_a)
		
		outreg2 using overspending_2002-2006_popshare_DK, addstat(Adj. R-squared, `r_sq_adj',max VIF, `max_vif') adec(3) bdec(2) sdec(2) word
		}
		set more on
	
**************************************************************************

**************************************************************************
* III. DDD-Analyses
**************************************************************************

* TABLE 3. DDD analysis of pre-merger overspending in a no-regulation scenario (Norway) vs. a regulation scenario (Denmark)
* TABLE A4. Full model
	
	global all_y net_current_exp_pc_fp_EUR gross_investments_pc_fp_EUR netresult_pc_fp_EUR liquidassets_pc_fp_EUR longtermdebt_pc_fp_EUR 
		
		set more off
		foreach Y of varlist $all_y {
		
		reg `Y' ///
			$amalgam ///
			$yeardum ///
			$context_var ///
			if filter_gen==1, cluster(ct_knr)
			
			fitstat
		
		estat vif
		local max_vif = `r(vif_1)'

		local r_sq_adj = e(r2_a)
		
* FIGURE 2 (Predictive margins for allocation of pre-merger overspending) and 
* FIGURE 3 (Predictive margins for financing of pre-merger overspending).
		margins i.year_of_analysis#i.amalgamation#country 
		marginsplot, recast(line) scheme(s1mono) title ("Predictive Margins with 95% CIs: `Y' ") title(" ", suffix) xtitle("year") ytitle("Predicted level") xline(3, lcolor(black) lpattern(dot))
		graph save Margins_`Y'.gph, replace
		
		outreg2 using overspending_DK_NO, addstat(Adj. R-squared, `r_sq_adj',max VIF, `max_vif') adec(3) bdec(2) sdec(2) word
		}
		set more on

* TABLE A6. DDD analysis of pre-merger overspending in a no-regulation scenario vs. a regulation-scenario, differentiated by the size of the municipality relative to the merged unit.
		
	global all_y net_current_exp_pc_fp_EUR gross_investments_pc_fp_EUR netresult_pc_fp_EUR liquidassets_pc_fp_EUR longtermdebt_pc_fp_EUR 
	
		set more off
		foreach Y of varlist $all_y {
		
		reg `Y' ///
			$amalgam_popshare ///
			$yeardum ///
			$context_popshare ///
			if filter_gen==1, cluster(ct_knr)
			
			fitstat
		
		estat vif
		local max_vif = `r(vif_1)'

		local r_sq_adj = e(r2_a)

		outreg2 using overspending_DK_NO_popshare, addstat(Adj. R-squared, `r_sq_adj',max VIF, `max_vif') adec(3) bdec(2) sdec(2) word
		}
		set more on
		
